using System.Configuration;
using System.Collections.Generic;

namespace MediaBackup
{
    // BrianSp: One place to load and store settings used by this assembly.
    // $$BrianSp: While convenient, this is not a very extensible strategy, especially for modular extensibility.
    public class Settings
    {
        public string TivoMak;

        public string TivoIpAddress;
        public string TivoDownloadRootPath;
        public string TivoDownloadUsername;
        public string TivoDownloadPassword;

        public string TivoDecrypter;
        public Dictionary<int, int> TivoDecrypterSuccessExitCodes = new Dictionary<int, int>();
        public ushort TivoDecrypterTimeoutMinutes;
        public string TivoDecryptRootPath;
        public string TivoDecryptUsername;
        public string TivoDecryptPassword;

        public string Transcoder;
        public Dictionary<int, int> TranscoderSuccessExitCodes = new Dictionary<int, int>();
        public ushort TranscoderTimeoutMinutes;
        public string TranscodedExtension;
        public string TranscodeRootPath;
        public string TranscodeUsername;
        public string TranscodePassword;

        public bool DoWork;

        // BrianSp: This loads from ConfigurationManager.AppSettings (obviously).
        // $$BrianSp: It might be slightly more elegant from a code point of view to use xml serialization instead.
        public void LoadFromAppSettings()
        {
            TivoMak = ConfigurationManager.AppSettings["TiVo MAK"];

            TivoIpAddress = ConfigurationManager.AppSettings["TiVo IpAddress"];
            TivoDownloadRootPath = ConfigurationManager.AppSettings["TiVo Download Root Path"];
            TivoDownloadUsername = ConfigurationManager.AppSettings["TiVo Download Username"];
            TivoDownloadPassword = ConfigurationManager.AppSettings["TiVo Download Password"];

            TivoDecrypter = ConfigurationManager.AppSettings["TiVo Decrypter"];
            TivoDecryptRootPath = ConfigurationManager.AppSettings["TiVo Decrypt Root Path"];
            TivoDecryptUsername = ConfigurationManager.AppSettings["TiVo Decrypt Username"];
            TivoDecryptPassword = ConfigurationManager.AppSettings["TiVo Decrypt Password"];

            Transcoder = ConfigurationManager.AppSettings["Transcoder"];
            TranscodedExtension = ConfigurationManager.AppSettings["Transcoded Extension"];
            TranscodeRootPath = ConfigurationManager.AppSettings["Transcode Root Path"];
            TranscodeUsername = ConfigurationManager.AppSettings["Transcode Username"];
            TranscodePassword = ConfigurationManager.AppSettings["Transcode Password"];

            string value = ConfigurationManager.AppSettings["Do Work"];
            DoWork = (value == null) ? true : bool.Parse(value);

            value = ConfigurationManager.AppSettings["TiVo Decrypter Success Exit Codes"];
            ParseCodes(value, TivoDecrypterSuccessExitCodes);

            value = ConfigurationManager.AppSettings["Transcoder Success Exit Codes"];
            ParseCodes(value, TranscoderSuccessExitCodes);

            value = ConfigurationManager.AppSettings["TiVo Decrypter Timeout Minutes"];
            TivoDecrypterTimeoutMinutes = (value == null) ? (ushort)60 : ushort.Parse(value);

            value = ConfigurationManager.AppSettings["Transcoder Timeout Minutes"];
            TranscoderTimeoutMinutes = (value == null) ? (ushort)(60 * 12) : ushort.Parse(value);
        }

        private static void ParseCodes(string value, Dictionary<int, int> set)
        {
            if (value != null)
            {
                string[] values = value.Split(',');
                foreach (string codeStr in values)
                {
                    int code = int.Parse(codeStr);
                    set[code] = code;
                }
            }
        }

        // BrianSp: Pure convenience helper, also not part of an elegantly extensible architecture.  Oh well.
        public void Authenticate()
        {
            Authenticate(TivoDownloadRootPath, TivoDownloadUsername, TivoDownloadPassword);
            Authenticate(TivoDecryptRootPath, TivoDecryptUsername, TivoDecryptPassword);
            Authenticate(TranscodeRootPath, TranscodeUsername, TranscodePassword);
        }

        private void Authenticate(string path, string username, string password)
        {
            if(path != null)
                WNet.Authenticate(path, username, password);
        }
    }
}
